<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Common_role_manage extends CI_Controller {


	public function __construct() {
		parent::__construct();
		session_start();
		$this->load->library("common/login_manage");
		$this->load->library("common/right_manage");
		$this->load->library("common/role_manage");
		$this->load->library("common/user_menu");
		$this->config->load("config_common");
	}

	public function index() {
		$this->role_list();
	}

	public function to_add_role() {
		$this->login_manage->redirect_url_check_login();
		$data['user_menu']=$this->user_menu->get_user_menu();
		if (!$this->right_manage->check_right("add_role",$_SESSION['user_id'])) {
			$data['title']		="权限不足";
			$data['message']	="你没有权限使用这个功能。";
			$this->load->view("common/error_tip",$data);
			return;
		}
		$this->load->view("common/role_manage/to_add_role",$data);
	}
	public function check_role_name() {
		$role_name	=trim($this->input->get_post("role_name"));
		$role_id	=trim($this->input->get_post("role_id"));
		$result		=$this->role_manage->check_role_name($role_name,$role_id);
		echo json_encode($result);
	}

	public function add_role() {
		if ($this->login_manage->check_login()==false) {
			$data['result']		=false;
			$data['message']	='请您先登录';
			echo json_encode($data);die();
		}
		if (!$this->right_manage->check_right("add_role",$_SESSION['user_id'])) {
			$result['result']	=false;
			$result['message']	='你没有权限使用这个功能';
			echo json_encode($result);die();
		}
		$data['role_name']		=trim($this->input->get_post("role_name"));
		$data['description']	=trim($this->input->get_post("description"));
		$result	=$this->role_manage->add_role($data);
		echo json_encode($result);
	}

	public function add_role_end() {
		$this->login_manage->redirect_url_check_login();
		$data['user_menu']=$this->user_menu->get_user_menu();
		$role_id	=$this->input->get_post("role_id");
		$role_info	=$this->role_manage->query_role_info($role_id);
		$data['title']		="新增角色成功";
		$data['message']	="新增角色 <strong>".$role_info['ROLE_NAME']."</strong>成功!";
		$data['back_url']	=base_url()."index.php/common_role_manage/to_add_role";
		$this->load->view("common/success_message",$data);
	}
	public function role_list() {
		$this->login_manage->redirect_url_check_login();
		$data['user_menu']	=$this->user_menu->get_user_menu();
		
		$data['update_role']	=false;
		if ($this->right_manage->check_right("update_role")) {
			$data['update_role']	=true;
		}
		$data['delete_role']	=false;
		if ($this->right_manage->check_right("delete_role")) {
			$data['delete_role']=true;
		}
		$data['assign_role_right']	=false;
		if ($this->right_manage->check_right("assign_role_right")) {
			$data['assign_role_right']=true;
		}
		$data['add_role']	=false;
		if ($this->right_manage->check_right("add_role")) {
			$data['add_role']	=true;
		}
		$data['query_role']	=false;
		if ($this->right_manage->check_right("query_role")) {
			$data['query_role']	=true;
		}
		$data['role_list'] 	=$this->role_manage->query_role_list();
		$data['role_num']	=$this->role_manage->query_role_num();
		$this->load->library("common/pagination");
		$data['page_link']	=$this->pagination->get_pagination(base_url()."index.php/common_role_manage/role_list?",$data['role_num']);
		$this->load->view("common/role_manage/role_list",$data);
	}

	public function to_assign_right() {
		$this->login_manage->redirect_url_check_login();
		$data['user_menu']	=$this->user_menu->get_user_menu();
		if (!$this->right_manage->check_right("assign_role_right",$_SESSION['user_id'])) {
			$data['title']		="权限不足";
			$data['message']	="你没有权限使用这个功能。";
			$this->load->view("common/error_tip",$data);
			return;
		}
		$role_id	=$this->input->get_post("role_id");
		$data['role_info'] 	=$this->role_manage->query_role_info($role_id);
		if ($data['role_info']==null) {
			$data['title']		="分配权限失败";
			$data['message']	="你选择的角色不存在!";
			$data['back_url']	=base_url()."index.php/common_role_manage/role_list";
			$this->load->view("common/error_tip",$data);
			return ;
		}
		$data['role_right']	=$this->role_manage->get_role_right($role_id);
		$this->load->library("common/right_manage");
		$data['right_list']	=$this->right_manage->query_right_list();
		$data['module_list']=$this->right_manage->get_module_right();
		$this->load->view("common/role_manage/to_assign_right",$data);
	}

	public function assign_right() {
		if ($this->login_manage->check_login()==false) {
			$data['result']		=false;
			$data['message']	='请您先登录';
			echo json_encode($data);die();
		}
		if (!$this->right_manage->check_right("assign_role_right")) {
			$result['result']	=false;
			$result['message']	='你没有权限使用这个功能';
			echo json_encode($result);die();
		}
		$role_id	=$this->input->get_post("role_id");
		$right_id	=$this->input->get_post("right_id");
		$result		=$this->role_manage->assign_right($role_id,$right_id);
		echo json_encode($result);
	}

	public function assign_right_end() {
		$this->login_manage->redirect_url_check_login();
		$data['user_menu']=$this->user_menu->get_user_menu();
		$role_id	=$this->input->get_post("role_id");
		$role_info	=$this->role_manage->query_role_info($role_id);
		$data['title']		="角色权限分配成功";
		$data['message']	="角色 <strong>".$role_info['ROLE_NAME']."</strong>的权限已经分配成功!";
		$data['back_url']	=base_url()."index.php/common_role_manage/role_list";
		$this->load->view("common/success_message",$data);
	}

	public function to_modify_role() {
		$this->login_manage->redirect_url_check_login();
		$data['user_menu']=$this->user_menu->get_user_menu();
		if (!$this->right_manage->check_right("update_role",$_SESSION['user_id'])) {
			$data['title']		="权限不足";
			$data['message']	="你没有权限使用这个功能。";
			$this->load->view("common/error_tip",$data);
			return;
		}
		$role_id	=$this->input->get_post("role_id");
		$data['role_info'] 	=$this->role_manage->query_role_info($role_id);
		if ($data['role_info']==null) {
			$data['title']		="编辑角色失败";
			$data['message']	="你要编辑的角色不存在!";
			$data['back_url']	=base_url()."index.php/common_role_manage/role_list";
			$this->load->view("common/error_message",$data);
			return ;
		}
		$this->load->view("common/role_manage/to_modify_role",$data);
	}

	public function modify_role() {
		if ($this->login_manage->check_login()==false) {
			$data['result']		=false;
			$data['message']	='请您先登录';
			echo json_encode($data);die();
		}
		if (!$this->right_manage->check_right("update_role")) {
			$result['result']	=false;
			$result['message']	='你没有权限使用这个功能';
			echo json_encode($result);die();
		}
		$data['role_name']		=trim($this->input->get_post("role_name"));
		$data['description']	=trim($this->input->get_post("description"));
		$role_id				=$this->input->get_post("role_id");
		$result	=$this->role_manage->modify_role($data,$role_id);
		echo json_encode($result);
	}


	public function to_delete_role() {
		$this->login_manage->redirect_url_check_login();
		$data['user_menu']=$this->user_menu->get_user_menu();
		if (!$this->right_manage->check_right("role_delete")) {
			$data['title']		="权限不足";
			$data['message']	="你没有权限使用这个功能。";
			$data['back_url']	=base_url()."index.php/common_right_manage";
			$this->load->view("common/error_tip",$data);
			return;
		}
		$role_id	=$this->input->get_post("role_id");
		$role_info 	=$this->role_manage->query_role_info($role_id);
		if ($role_info==null) {
			$data['title']		="删除角色失败";
			$data['message']	="你要删除的角色不存在!";
			$data['back_url']	=base_url()."index.php/common_role_manage/role_list";
			$this->load->view("common/error_message",$data);
			return ;
		}

		$data['title']			="确认删除角色";
		$data['message']		="是否确认删除角色 <strong>".$role_info['ROLE_NAME']."</strong>?";
		$data['back_url']		=base_url()."index.php/common_role_manage/role_list";
		$data['forward_url']	=base_url()."index.php/common_role_manage/delete_role?role_id=".$role_id;
		$this->load->view("common/warning_message",$data);
		return ;
	}

	public function delete_role() {
		$this->login_manage->redirect_url_check_login();
		$data['user_menu']=$this->user_menu->get_user_menu();
		if (!$this->right_manage->check_right("delete_role")) {
			$data['title']		="权限不足";
			$data['message']	="你没有权限使用这个功能。";
			$this->load->view("common/error_tip",$data);
			return;
		}
		$role_id	=$this->input->get_post("role_id");
		$role_info 	=$this->role_manage->query_role_info($role_id);
		if ($role_info==null) {
			$data['title']		="删除角色失败";
			$data['message']	="你要删除的角色不存在!";
			$data['back_url']	=base_url()."index.php/common_role_manage/role_list";
			$this->load->view("common/error_message",$data);
			return ;
		}
		$result 	=$this->role_manage->delete_role($role_id);
		if ($result['result']==false) {
			$data['title']		="删除角色失败";
			$data['message']	=$result['message'];
			$data['back_url']	=base_url()."index.php/common_role_manage/role_list";
			$this->load->view("common/error_message",$data);
			return ;
		}
		echo "<script>history.go(-1)</script>";
	}
	public function search_role() {
		$this->login_manage->redirect_url_check_login();
		$data['user_menu']		=$this->user_menu->get_user_menu();
		if (!$this->right_manage->check_right("role_query")) {
			$data['title']		="权限不足";
			$data['message']	="你没有权限使用这个功能。";
			$data['back_url']	=base_url()."index.php/common_right_manage";
			$this->load->view("common/error_message",$data);
			return;
		}
		$data['role_update']	=true;
		if ($this->right_manage->check_right("role_update")) {
			$data['role_update']	=true;
		}
		$data['role_delete']	=false;
		if ($this->right_manage->check_right("role_delete")) {
			$data['role_delete']=true;
		}
		$data['key'] 			=$this->input->get("key");
		if ($data['key']==null) {
			$data['role_list']	=null;
			$data['role_num']	=0;
		}else{
			$data['role_list']	=$this->role_manage->search_role($data['key']);
			$data['role_num']	=count($data['role_list']);
		}
		$this->load->view("common/role_manage/search_role",$data);
	}
}